<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img{
            width: 50px;
            height: 50px;
            position: absolute;
            border: 3px solid #000;
        }

    </style>
</head>
<body>

    <img src="../images/6.jpg" alt="">

    <script>

        // 鼠标中心跟随  减去盒子自身宽高的一半
        var oImg = document.querySelector('img') ;

        var r = oImg.offsetWidth / 2 ;   // 只获取一次

        document.onmousemove = function(e) {
            var e = e || event ;
            var x = e.x ;
            var y = e.y ;
            oImg.style.left = x - r + 'px' ;
            oImg.style.top = y - r + 'px' ;
        }

    </script>
    
</body>
</html>